package org.jflame.context.spring;

import org.jflame.commons.config.EncryptedValueResolver;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

/**
 * 扩展PropertySourcesPlaceholderConfigurer,支持加密属性
 * 
 * @see MyPropertySourceFactory
 * @author charles.zhang
 */
public class EncryptedPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {

    private EncryptedValueResolver valueResolver;

    @Override
    protected String convertPropertyValue(String originalValue) {
        if (valueResolver != null) {
            return valueResolver.resolve(originalValue);
        }
        return originalValue;
    }

    /**
     * 启用加密值解析支持,默认使用PBE解密
     * 
     * @param password 密钥
     */
    public void enableEncryptedValueResolver(String password) {
        valueResolver = EncryptedValueResolver.buildByPBE(password);
    }

    public void setEncryptedValueResolver(EncryptedValueResolver _encryptedValueResolver) {
        valueResolver = _encryptedValueResolver;
    }

}
